package b2c;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.DefaultResourceLoader;

import com.google.common.collect.Maps;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 根据domain生成相应的dao、service代码工具
 * 只需要修改三个变量值
 * 		systemId  系统类型  {@code com.mall.common.enums.SystemID}}
 * 		className 首字母小写，不加Domain后缀
 * 		description 描述
 * 		author 开发者名称
 */
public class Generate {
	
	//*********************需要修改代码*****************************************
	static String className = "deliveryMethod";			// 类名，例：deliveryCorp
	static String description = "配送方式";			// 功能描述，例：物流公司
	static String author = "lzx";                      // 开发者名称  
	static String systemId = "b2c";                 //系统类型  {@code com.mall.common.enums.SystemID}}
	static String domainModelName = "delivery";       // 没有则为空，com.mall.b2c.domain.${模块名称}.MemberDomain
	//***********************************************************************
	
	public static void main(String[] args) throws Exception {
		if(StringUtils.isNotBlank(domainModelName)){
			domainModelName = domainModelName + ".";
		}else{
			domainModelName = "";
		}
		String domainPackage ="com.mall."+systemId+".domain."+domainModelName+StringUtils.capitalize(className)+"Domain";
		// 获取文件分隔符
		String separator = File.separator;
		
		// 获取工程路径
		File projectPath = new DefaultResourceLoader().getResource("").getFile();
		while(!new File(projectPath.getPath()+separator+"src"+separator+"main").exists()){
			projectPath = projectPath.getParentFile();
		}
		
		// 模板文件路径
		String tplPath = StringUtils.replace(projectPath+"/src/test/java/"+systemId+"/template", "/", separator);
		
		// Java文件路径
		String javaPath = StringUtils.replaceEach(projectPath+"/src/main/java/com/mall."+systemId,
				new String[]{"/", "."}, new String[]{separator, separator});
		
		// 代码模板配置
		Configuration cfg = new Configuration();
		cfg.setDefaultEncoding("UTF-8");
		cfg.setDirectoryForTemplateLoading(new File(tplPath));

		// 定义模板变量
		Map<String, String> model = Maps.newHashMap();
		model.put("systemId", systemId);
		model.put("author", author);
		model.put("className", StringUtils.uncapitalize(className));
		model.put("ClassName", StringUtils.capitalize(className));
		model.put("description", description);
		model.put("domainPackage", domainPackage);

		// 生成 Dao
		Template template = cfg.getTemplate("dao.ftl");
		String content = FreeMarkers.renderTemplate(template, model);
		String filePath = javaPath+separator+"dao"+separator+model.get("ClassName")+"Dao.java";
		writeFile(content, filePath);
		
		// 生成 DaoImpl
		template = cfg.getTemplate("daoimpl.ftl");
		content = FreeMarkers.renderTemplate(template, model);
		filePath = javaPath+separator+"dao"+separator+"impl"+separator+model.get("ClassName")+"DaoImpl.java";
		writeFile(content, filePath);
		
		// 生成 Service
		template = cfg.getTemplate("service.ftl");
		content = FreeMarkers.renderTemplate(template, model);
		filePath = javaPath+separator+"service"+separator+model.get("ClassName")+"Service.java";
		writeFile(content, filePath);
		
		// 生成 ServiceImpl
		template = cfg.getTemplate("serviceimpl.ftl");
		content = FreeMarkers.renderTemplate(template, model);
		filePath = javaPath+separator+"service"+separator+"impl"+separator+model.get("ClassName")+"ServiceImpl.java";
		writeFile(content, filePath);
		
	}
	
	/**
	 * 将内容写入文件
	 * @param content
	 * @param filePath
	 */
	public static void writeFile(String content, String filePath) {
		try {
//			if (b2c.FileUtils.createFile(filePath)){
				FileOutputStream fos = new FileOutputStream(filePath);
				Writer writer = new OutputStreamWriter(fos,"UTF-8");
				BufferedWriter bufferedWriter = new BufferedWriter(writer);
				bufferedWriter.write(content);
				bufferedWriter.close();
				writer.close();
//			}else{
//				System.out.println("创建失败");
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
